package com.example.send.util;

import java.util.HashMap;
import java.util.Map;

public class DivisionUtil {

    public static void main(String[] args) throws InterruptedException {
        Map<String,Double> geMap = new HashMap<>();
        for(int i = 1; i <= 9 ; i++) {
            for(int j = 1; j <= 9 ; j++) {
                double value = Math.round(i*100/j) / 100.0;
                System.out.println(i+ "/" + j + " " + value);
                geMap.put(i+ "/" + j, value);
            }
        }
        while (true) {


            System.out.println("********************************************");
            int v1 = (int)(100+Math.random()*(999-100+1));
            int v2 = (int)(100+Math.random()*(999-100+1));

//            System.out.println(v1 + " / " + v2);
            System.out.println(v1);
            System.out.println("---");
            System.out.println(v2);
            String v1Str = String.valueOf(v1);
            String v2Str = String.valueOf(v2);

            int v1_1 = Integer.valueOf(String.valueOf(v1Str.charAt(0)));
            int v1_2 = Integer.valueOf(String.valueOf(v1Str.charAt(1)));
            int v1_3 = Integer.valueOf(String.valueOf(v1Str.charAt(2)));

            int v2_1 = Integer.valueOf(String.valueOf(v2Str.charAt(0)));
            int v2_2 = Integer.valueOf(String.valueOf(v2Str.charAt(1)));
            int v2_3 = Integer.valueOf(String.valueOf(v2Str.charAt(2)));

            String first = v1_1 + "/" + v2_1;
            double firstResult = geMap.get(first);
            System.out.println("STEP1: 计算" + first + " = " + firstResult);
            int v2_2_s = (v2_3 > 5 ? v2_2 + 1 : v2_2)*10;
            long secondResult = Math.round(v2_2_s * firstResult);
            System.out.println("STEP2: 计算" + v2_2_s + " * " + firstResult + " = " + secondResult);
            double thirdResult = (v1_2*10 + v1_3) - secondResult;
            String pre = "";
            int suf = 1;
            if (thirdResult < 0) {
                thirdResult = -thirdResult;
                pre = "-";
                suf = -1;
            }
            System.out.println("STEP3: 计算" + (v1_2*10 + v1_3) + " - " + secondResult + " = " + pre + thirdResult);
            long thirdResult_s = Math.round(thirdResult/10.0);
            long v2_1_s = v2_2 > 5 ? v2_1 + 1 : v2_1;
            double s = 1.0;
            String forth = pre + thirdResult_s + "/" + v2_1_s;
            if (thirdResult_s > 10) {
                thirdResult_s = Math.round(thirdResult/100.0);
                s *= 10;
            } else if (thirdResult_s < 1) {
                thirdResult_s = Math.round(thirdResult);
                s *= 0.1;
            }
            if (v2_1_s > 10) {
                v2_1_s = Math.round(v2_1_s/10.0);
                s *= 10;
            }
            String forth_s = thirdResult_s + "/" + v2_1_s;
            double forthResult = geMap.get(forth_s) * s;
            System.out.println("STEP4: 计算" + forth + " = " + pre + forthResult);
            double result = firstResult + suf*forthResult/10;
            System.out.println("STEP5: 计算" + firstResult + " + " + pre + forthResult/10 + " = " + result);
            int estimateVal = (int)Math.floor(result*1000.0);
            System.out.println("estimate: " + estimateVal/10.0 + "%");
            int intVal = (int)Math.floor(v1*1000.0/v2);
            System.out.println("real: " + intVal/10.0 + "%");
            System.out.println("********************************************");
            Thread.sleep(10000);
        }


    }

}
